package com.itbaima.edu.info.manager.controller;

import com.itbaima.edu.info.manager.domain.Student;
import com.itbaima.edu.info.manager.service.StudentService;

import java.util.Scanner;

/**
 * Created by byakuya on 2020/8/5
 */
public class StudentController {  //客服

    private final Scanner sc = new Scanner(System.in);
    private final StudentService studentService = new StudentService();
    //启动学生管理系统，并展示学生管理系统菜单
    public void start() {

        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:  //添加学生
                    addStudent();
                    break;
                case 2:  //删除学生
                    deleteStudentById();
                    break;
                case 3:  //修改学生
                    updateStudent();
                    break;
                case 4:  //查看学生
                    findAllStudent();
                    break;
                case 5:  //告辞
                    System.out.println("告辞");
                    return;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    public void updateStudent() {

        String updateId = inputStudentId();
        Student newStu = inputStudentInfo(updateId);

        studentService.updateStudent(updateId, newStu);
        System.out.println("修改成功！");

    }

    public void deleteStudentById() {
        String delId = inputStudentId();

        //调用业务员中的deleteStudentById()
        studentService.deleteStudentById(delId);
        System.out.println("删除成功！");
    }

    public void findAllStudent() {
        //调用业务员中的获取方法
        Student[] stus = studentService.findAllStudent();
        //判断数组内存地址是否为null
        if (stus == null){
            System.out.println("查无信息，请添加后重试");
        }else {
            //遍历数组
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (Student student : stus) {
                if (student != null)
                    System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getBirthday());
            }
        }
    }

    public void addStudent() {

        //键盘录入待添加学生信息
        String id;
        while (true) {
            System.out.println("请输入学生ID");
            id = sc.next();
            //判断ID是否存在
            boolean flag = studentService.isExists(id);
            if (flag){
                System.out.println("ID已被占用，请重新输入");
            }else break;
        }

        Student stu = inputStudentInfo(id);

        boolean result = studentService.addStudent(stu);
        if (result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

    }

    //键盘录入已经存在的学生id
    public String inputStudentId() {
        String id;
        while (true) {
            //键盘录入待删除ID。判断是否存在
            System.out.println("请输入学生ID");
            id = sc.next();
            //判断是否存在
            if (!studentService.isExists(id)) {
                System.out.println("ID不存在，请重新输入");
            }else break;
        }
        return id;
    }

    //键盘录入学生信息，封装为学生对象
    public Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生生日");
        String birthday = sc.next();

        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }
}
